package com.zy.jike.数据结构与算法之美.基本数据结构.queue;

/**
 * @ClassName QueueBasedOnLinkedList
 * @Description 基于链表实现队列
 * @Author peppers
 * @Date 2020/3/24
 * @Version 1.0
 **/
public class QueueBasedOnLinkedList {
    private static class Node {
        private String data;
        private Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }

        public String getData() {
            return data;
        }
    }

    //队列的队首和队尾
    private Node head = null;
    private Node tail = null;

    //入队
    public void enqueue(String value){
        if(tail == null){
            Node  newNode = new Node(value,null);
            head = newNode;
            tail = newNode;
        }else {
            tail.next = new Node(value,null);
            tail = tail.next;
        }
    }

    //出队
    public String dequeue(){
        if(head ==null){
            return null;
        }
        String value = head.data;
        head = head.next;
        if(head ==null){
            tail =null;
        }
        return value;
    }
    public void printAll() {
        Node p = head;
        while (p != null) {
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }
}
